package chapter_05;


class Link3 {
    // 节点
    public long dData;
    public Link3 next;

    public Link3(long dd) {
        dData = dd;
    }

    public void disPlayLink() {
        System.out.print(dData + " ");
    }
}


class LinkList3 {
    // 链表
    private Link3 first;

    public LinkList3() {
        first = null;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public void insertFirst(long dd) {
        Link3 newLink = new Link3(dd);
        newLink.next = first;
        first = newLink;
    }

    public long deleteFirst() {
        Link3 temp = first;
        first = first.next;
        return temp.dData;
    }

    public void displayList() {
        Link3 current = first;
        while (current != null) {
            current.disPlayLink();
            current = current.next;
        }
        System.out.println();
    }
}


public class LinkStack {
    // 栈
    private LinkList3 theList;

    public LinkStack() {
        theList = new LinkList3();
    }

    public void push(long j) {
        theList.insertFirst(j);
    }

    public long pop() {
        return theList.deleteFirst();
    }

    public boolean isEmpty() {
        return theList.isEmpty();
    }

    public void displayStack() {
        System.out.print("Stack (top-->bottom): ");
        theList.displayList();
    }
}


class LinkStackApp {

    public static void main(String[] args) {
        LinkStack theStack = new LinkStack();
        theStack.push(20);
        theStack.push(40);
        theStack.displayStack();
        theStack.push(60);
        theStack.push(80);
        theStack.displayStack();

        theStack.pop();
        theStack.pop();
        theStack.displayStack();
    }
}
